﻿using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;

namespace Quantum.Logging
{
    public class SqlServerLoggerProvider : ILoggerProvider
    {
        private readonly ConcurrentDictionary<string, SqlServerLogger> _loggers = new ConcurrentDictionary<string, SqlServerLogger>();
        private readonly Func<string, LogLevel, bool> _filter;
        private static readonly Func<string, LogLevel, bool> trueFilter = (cat, level) => true;
        private static readonly Func<string, LogLevel, bool> falseFilter = (cat, level) => false;

        public SqlServerLoggerOptions LoggerOptions { get; set; }

        public void Dispose()
        {
        }

        public ILogger CreateLogger(string name)
        {
            return _loggers.GetOrAdd(name, new SqlServerLogger(name, LoggerOptions));
        }

        private Func<string, LogLevel, bool> GetFilter(string name)
        {
            if (_filter != null)
            {
                return _filter;
            }

            return falseFilter;
        }
    }
}
